package com.ruoyi.user.service.impl;

import com.repair.api.user.request.UserQueryPageRequest;
import com.repair.api.user.request.UserQueryRequest;
import com.repair.api.user.request.UserUpdateStatusRequest;
import com.repair.api.user.request.param.UserQueryParam;
import com.repair.api.user.request.param.UserUpdateStatusParam;
import com.repair.api.user.response.UserQueryResponse;
import com.repair.api.user.response.data.UserInfo;
import com.repair.api.user.service.UserFacadeService;
import com.repair.base.exception.BizErrorCode;
import com.repair.base.exception.BizException;
import com.repair.base.response.NoResultResponse;
import com.repair.base.response.PageResponse;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.user.service.AdminUserService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

/**
 * @author chunyu
 * @since 2024/9/27,21:26
 */
@Service
public class AdminUserServiceImpl implements AdminUserService {

    @DubboReference
    private UserFacadeService userFacadeService;

    @Override
    public TableDataInfo getAllUsers(UserQueryParam userQueryParam) {

        UserQueryPageRequest<UserQueryParam> queryParam = new UserQueryPageRequest<>();
        queryParam.setData(userQueryParam);
        queryParam.setCurrentPage(userQueryParam.getCurrentPage());
        queryParam.setPageSize(userQueryParam.getPageSize());
        PageResponse<UserInfo> pageResponse = userFacadeService.getAllUsers(queryParam);
        if (!pageResponse.getSuccess()) {
            throw new BizException(BizErrorCode.REMOTE_CALL_RESPONSE_IS_FAILED);
        }
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setTotal(pageResponse.getTotal());
        tableDataInfo.setRows(pageResponse.getData());
        return tableDataInfo;
    }

    @Override
    public UserInfo selectUsers(Long id) {

        UserQueryRequest request = new UserQueryRequest(id);
        UserQueryResponse<UserInfo> response = userFacadeService.query(request);
        if (!response.getSuccess()) {
            throw new BizException(BizErrorCode.REMOTE_CALL_RESPONSE_IS_FAILED);
        }
        return response.getData();
    }

    @Override
    public UserInfo updateState(Long id, String state) {
        final UserInfo userInfo = new UserInfo();
        userInfo.setId(id);
        userInfo.setState("0");
       UserQueryResponse<UserInfo> userQueryResponse =userFacadeService.query(new UserQueryRequest(state));
        if (!userQueryResponse.getSuccess()){
            throw new BizException(BizErrorCode.NOTICE_SAVE_FAILED);
        }
        return userQueryResponse.getData();
    }

    @Override
    public Boolean blockOrUnBlockUsers(UserUpdateStatusParam param) {
        UserUpdateStatusRequest<UserUpdateStatusParam> request = new UserUpdateStatusRequest<>();
        request.setData(param);
        NoResultResponse response = userFacadeService.updateUserStatus(request);
        if (!response.getSuccess()) {
            throw new BizException(BizErrorCode.REMOTE_CALL_RESPONSE_IS_FAILED);
        }
        return true;
    }
}
